/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import JmesPath from "./jmespath.js";

const basic = {
  name: '基本表达式',
  expression: 'a.b.c[0].d[1][0]',
  case: `{"a": {
      "b": {
        "c": [
          {"d": [0, [1, 2]]},
          {"d": [3, 4]}
        ]
      }
    }}`,
  result: '1'
}

let string = `tpqsfxqwjhjrhgoptzvijchvkutozjcyoazhshvtrywslmbgxhmwexastkvjaoxwzandssxpcwhbzunbnqkweotdiaxgkljafgosvumrnplkwqlvwdtenkazuwezvxveuujtbryolgocltdthewstgydxnzqdjsgxrnuwllaadyxsperixrzmpzkqkvdiyvkujxohforpincllqyzqjsfiufbcicuapvyipdtteomqyiivrvuxdhtipflzopnpnrmpicloovotkvvaentscdwrndisdvrdvbclhwrfmkjpvhkuvibxsxwfirvpsjwmaqpiaxztcjjqzykcevjbojhsyhcravmydyynsxwtawcynlmqrbmddutmhszrcgszlgxzxsplhgqdgjuekzdufofuohxqzxgxzmexlqxoehhfgwdaldymxqcryipjtupdobdepahbpanubblzgvobjvxvhzyrbcixunrqoahtviytaufigttqbdnpaayjphmnjthigxocffdtmkqbfarbuoawffmfgbzzxsddisjcqkouhjcswxrijfcwppximnxpayiymqelnlwpdydbfeyprqqqcrkwxnnjvivnunirzeqbxdrrqtjpyqwssiwmzagnabcxwrjeuqknitigxdyudssgwppvqayobzrtnrqeqjthpnagbtrmwylqoiarcihcachupprnqieuwmafhdtpnqwvtosuttsruulyadoftjcerakfwgzpcmqfltpyjiwfwtvtixdjaholvxhzfykthvkrsprisggnsyrrxjaofkbpskwjihqxdtxfkirsorruktadwydrwtcknplvpeexjavzlbbfokrpewtfaytjtnfcgygjyyhjgfftuskjkthrzwtxcdegjzbfgohmexvqn`;
let tokenList = ['maybe', "apple", '84576', "banana", "orange", '42',
  '7',
  'alpha',
  'beta',
  'gamma',
  'delta',
  'epsilon'];

const forNumber2 = 10000;
const forNumber3 = 1000;
function search_test() {
  let value = JSON.parse(basic.case)
  let result;
  let startTime = Date.now()
  for (let i = 0; i < forNumber3; i++) {
    result = JmesPath.search(value, basic.expression)
  }
  let endTime = Date.now()

  print(`jmespath_search: ${endTime - startTime} ms`)
  if (result != "1") throw Error(`jmespath_search != "1"`);
}

function tokenize_test() {
  let value;
  let startTime = Date.now()

  for (let i = 0; i < forNumber3; i++) {
    value = JmesPath.tokenize(string);
  }
  let endTime = Date.now()
  print(`jmespath_tokenize: ${endTime - startTime} ms`)
  if (value[0].value != string) throw Error(`jmespath_search != ${string}`);
}

function tokenize_test_02() {
  let value;
  let startTime = Date.now()

  for (let i = 0; i < forNumber2; i++) {
    value = JmesPath.tokenize(tokenList);
  }
  let endTime = Date.now()
  print(`jmespath_tokenize(List): ${endTime - startTime} ms`)
  if (value[0].value != tokenList.toString()) throw Error(`jmespath_tokenize != ${tokenList.toString()}`);
}
function compile_test() {
  let result;
  let startTime = Date.now()

  for (let i = 0; i < forNumber3; i++) {
    result = JmesPath.compile(string);
  }
  let endTime = Date.now()
  print(`jmespath_compile: ${endTime - startTime} ms`)
  if (result.name != string) throw Error(`jmespath_compile != ${string}`);
}
function compile_test_02() {
  let result;
  let startTime = Date.now()
  let compileList = ['maybe', '43690', 'apple', '84576', "banana", "orange", 'yes',
    '12345',
    'grape',
    'lemon',
    'kiwi',
    'strawberry']
  for (let i = 0; i < forNumber2; i++) {

    result = JmesPath.compile(compileList)
  }
  let endTime = Date.now()
  print(`jmespath_compile(List): ${endTime - startTime} ms`)
  if (result.name != compileList.toString()) throw Error(`jmespath_compile(List) != ${compileList.toString()}`);
}
function strictDeepEqual_test() {
  let result;
  let startTime = Date.now()

  for (let i = 0; i < forNumber2; i++) {
    result = JmesPath.strictDeepEqual([[0, 1], [2, 3]], [[0, 1], [2, 3]])
  }
  let endTime = Date.now()
  print(`jmespath_strictDeepEqual: ${endTime - startTime} ms`)
  if (result != true) throw Error(`jmespath_strictDeepEqual != true`);
}
function strictDeepEqual_test_02() {
  let result;
  let startTime = Date.now()
  for (let i = 0; i < forNumber2; i++) {
    result = JmesPath.strictDeepEqual({
      number: 3,
      plain: 'string',
      block: 'two\nlines\n',
      booleanTrue: true,
      booleanFalse: false,
      nullValue: null,
      undefinedValue: undefined,
      array: [1, 2, 3, 4, 5], // 一个数字数组
      nestedArray: [[6, 7], [8, 9]],
      objectNested: {
        a: 'inside object',
        b: 42,
        nestedObject: {
          x: 'deep inside',
          y: 100
        }
      }
    },
      {
        number: 3,
        plain: 'string',
        block: 'two\nlines\n',
        booleanTrue: true,
        booleanFalse: false,
        nullValue: null,
        undefinedValue: undefined,
        array: [1, 2, 3, 4, 5], // 一个数字数组
        nestedArray: [[6, 7], [8, 9]],
        objectNested: {
          a: 'inside object',
          b: 42,
          nestedObject: {
            x: 'deep inside',
            y: 100
          }
        }
      }
    )
  }
  let endTime = Date.now()
  print(`jmespath_strictDeepEqual(Map): ${endTime - startTime} ms`)
  if (result != true) throw Error(`jmespath_strictDeepEqual != true`);
}
search_test()
tokenize_test()
tokenize_test_02()
compile_test()
compile_test_02()
strictDeepEqual_test()
strictDeepEqual_test_02()